<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-06-22
 * Time: 23:31
 */

try{
    set_time_limit(0);
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Shanghai');
    require_once "../../db/video_db.php";

    $postData = file_get_contents('php://input');
    $data = json_decode($postData, true);

    $pattern = '/(\s)+/is';
    $uid = isset($data["uid"]) ? $data["uid"] : 0;

    $post_id = isset($data["post_id"]) ? $data["post_id"] : "";
    $comment_id = isset($data["comment_id"]) ? $data["comment_id"] : 0;

    $desc = isset($data["desc"]) ? $data["desc"] : "";
    $desc = str_replace('\'', '', $desc);
    $desc = trim($desc);
    $desc = preg_replace($pattern, "\n", $desc);

    $add_time = !empty($data["add_time"]) ? (int)$data["add_time"] : time();
    $add_time = !empty($add_time) ? $add_time : time();
    $like_num = isset($data["like_num"]) ? (int)$data["like_num"] : 0;
    $type = 2;
    $source = isset($data["source"]) ? $data["source"] : "";

    if(empty($post_id)){//解析帖子id
        $res = array("code"=>-1, "result"=>null, "msg"=>"帖子id不能为空");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    if(empty($desc)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"评论内容为空");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = "select * from t_post where id=".$post_id;
    $postInfo = video_db::fetch($sql);
    if(empty($postInfo)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"主贴不存在");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = 'select count(*) as num from t_user where uid='.$uid;
    $uNum = video_db::fetch($sql);
    if($uNum["num"] <= 0){
        $res = array("code"=>-1, "result"=>null, "msg"=>"用户不存在");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $to_uid = 0;
    if(!empty($comment_id)){//解析评论id
        $sql = "select * from t_comment where id=".$comment_id;
        $to_info = video_db::fetch($sql);
        $to_uid = $to_info['uid'];
    }
    $comment_id = (int)$comment_id;
    $sql = "insert into t_comment(uid, `type`, `desc`, post_id, comment_id, like_num, to_uid, add_time, source, status)values";
    $sql.= "('".$uid."','".$type."','".$desc."','".$post_id."','".$comment_id."','".$like_num."','".$to_uid."','".$add_time."','".$source."', 1)";
    $reply_id = video_db::execute($sql);
    if(empty($reply_id)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"发布评论失败");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $code = $add_time.$reply_id;
    $sql = "update t_comment set code='".$code."' where id=".$reply_id;
    video_db::execute($sql);

    $sql = "update t_post set comment_num=comment_num+1,last_reply_id=".$reply_id.", last_reply_time='".$add_time."' where id=".$post_id;
    video_db::execute($sql);

    $sql = "update t_user_count set reply_num=reply_num+1 where uid=".$uid;
    video_db::execute($sql);

    $sql = "select * from t_post_topic where post_id=".$postInfo["id"]." and status=1";
    $topicList = video_db::fetchAll($sql);
    $topicIds = array();
    foreach ($topicList as $topic){
        $topicIds[] = $topic["topic_id"];
    }
    if(!empty($topicIds)){
        $topic_ids = implode(",", $topicIds);
        $sql = "update t_topic set reply_num=reply_num+1 where id in (".$topic_ids.")";
        video_db::execute($sql);
    }

    $res = array("code"=>0, "result"=>array("reply_id" => $reply_id), "msg"=>"");
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;

}catch(PDOException $pe){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$pe->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$pe->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}catch (Exception $e){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$e->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$e->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}
